﻿/// <reference path="../../NiceValidator/jquery.validator.min.js" />
/// <reference path="../../KennyPublic.js" />
/// <reference path="../../gird.public.js" />
; (function () {
    "use strict";
    $(function () {
        page.init();
    });
    var _bt = window.BT;
    var page = {
        init: function () {
            this.event.init();
        },
        ajax: _bt.ajax,
        dialog: _bt.dialog,
        event: {},
        config: {
            grid: {
                id: '#grid'
                , selection: true
                , rowSelect: true
                , url: '/SystemManage/GetClassifyManageList'
                , formatters: {
                    'Operate': function (column, row) {
                        return '<a href="#" data-btn="edit" data-row-id="' + row['Id'] + '">修改</a>'
                    }
                }
                , events: [{ name: 'loaded', event: totalShow }]
            }
        }
    };
    page.grid = new _bt.BGrid(page.config.grid, null, true);
    page.event.init = function () {
        //查询
        $('#btn-search').on('click', search);
        //院系分类
        $('#btn-set-deparment').on('click', classifyDepartment);
        //联系人分类
        $('#btn-contact').on('click', classifyLink);
        //修改
        $(page.config.grid.id + ' tbody').on('click', "[data-btn='edit']", edit);
        //日历控件绑定
        $('.time-container').daterangepicker({
            language: 'zh-CN',
            alwaysShowCalendars: false,
            locale: {
                format: "YYYY-MM-DD"
            }
        }).prev().on(ace.click_event, function () {
            $(this).next().focus();
        });
    }
    //查询
    function search() {
        var param = $('#search-form').serializeObject();
        page.grid.reload(param);
    }
    //专利信息条数
    function totalShow() {
        var total = page.grid.getCount();
        $('#span-total').text("专利信息：共{0}条".format(total));
    }
    //院系分类
    function classifyDepartment() {
        var formId = '#form-deparment';
        var selection = getSelection();
        if (!selection['isSuccess']) {
            return;
        }
        var opt = {
            title: '院系分类',
            message: function (dialog) {
                var $messager = $($('#department-template').html());
                return $messager;
            },
            onshown: function (dialog) {
                var $form = $(formId);
                $form.validator({
                    valid: function (form) {
                        submitData(form, '/SystemManage/AddPatentDeparetment', dialog);
                    }
                });
                loadData({ id: selection['id'] }, function (data) {
                    $form.bindForm(data);
                })
            }
        };
        var successEvent = function (dialog) {
            $(formId).trigger('validate');
        }
        page.dialog.show(opt, successEvent);
        return false;
    }
    //联系方式分类
    function classifyLink() {
        var formId = '#form-contact';
        var selection = getSelection();
        if (!selection['isSuccess']) {
            return;
        }
        var opt = {
            title: '联系方式分类',
            message: function (dialog) {
                var $messager = $($('#contact-template').html());
                return $messager;
            },
            onshown: function (dialog) {
                var $form = $(formId)
                $form.validator({
                    valid: function (form) {
                        submitData(form, '/SystemManage/AddPatentContact', dialog);
                    }
                });
                loadData({ id: selection['id'] }, function (data) {
                    $form.bindForm(data);
                })
            }
        };
        var successEvent = function (dialog) {
            $(formId).trigger('validate');
        }
        page.dialog.show(opt, successEvent);
        return false;
    }
    //修改
    function edit() {
        var self = $(this);
        var id = self.data('rowId');
        var formId = '#form-edit';
        var opt = {
            title: '专利分类修改',
            message: function (dialog) {
                var $messager = $($('#edit-template').html());
                return $messager;
            },
            onshown: function (dialog) {
                var $form = $(formId);
                $form.validator({
                    valid: function (form) {
                        submitData(form, '/SystemManage/Edit', dialog);
                    }
                });
                loadData({ 'Id': id }, function (data) {
                    $form.bindForm(data);
                });
            }
        };
        var successEvent = function (dialog) {
            $(formId).trigger('validate');
        }
        page.dialog.show(opt, successEvent);
        return false;
    }
    //获取编辑的数据
    //param  success回调
    function loadData(data, success) {
        var ajaxError = function () {
            page.dialog.warn('加载数据失败');
        }
        page.ajax({
            url: '/SystemManage/GetPatentInfo',
            data: data
        }, function (d) {
            if (d && d.isSuccess) {
                success && success(d.data);
            } else {
                ajaxError();
            }
        }, ajaxError);
    }
    //提交数据
    function submitData(form, url, dialog) {
        page.ajax({
            url: url,
            data: $(form).serialize()
        }, function (data) {
            if (data && data.isSuccess) {
                page.dialog.info('保存成功');
                dialog.close();
                search();
            } else {
                page.dialog.warn(data.msg || '保存失败');
            }
        })
    }
    //获取选中行
    function getSelection() {
        var result = { isSuccess: true };
        var selection = page.grid.getSelections();
        if (selection.length == 0) {
            page.dialog.warn('请选择一条数据');
            result.isSuccess = false;
            return result;
        }
        if (selection.length > 1) {
            page.dialog.warn('只能选择一条数据');
            result.isSuccess = false;
            return result;
        }
        result['id'] = selection[0];
        return result;
    }
})();